define(['./../../initial'],
    function() {
        var app = angular.module('App.home');
        app.controller('user.infoCtrl', ['$scope', '$rootScope', '$state', '$stateParams', '$timeout', 'User', 'showMsg',
            function($scope, $rootScope, $state, $stateParams, $timeout, User, showMsg) {

                $scope.avatarUrl = "";

                var initUploader = function() {
                    var uploader = Qiniu.uploader({
                        runtimes: 'html5,flash,html4', // 上传模式，依次退化
                        browse_button: 'avatarUpload', // 上传选择的点选按钮，**必需**
                        uptoken_url: '/token/avatar',
                        unique_names: true,
                        domain: 'http://jigi-imgs.qiniudn.com/',
                        container: 'container', // 上传区域DOM ID，默认是browser_button的父元素
                        max_file_size: '50kb', // 最大文件体积限制
                        flash_swf_url: 'javascripts/bin/plupload/Moxie.swf', // 引入flash，相对路径
                        max_retries: 3, // 上传失败最大重试次数
                        dragdrop: false, // 开启可拖拽上传
                        chunk_size: '4mb', // 分块上传时，每片的体积
                        auto_start: true, // 选择文件后自动上传，若关闭需要自己绑定事件触发上传
                        init: {
                            'FilesAdded': function(up, files) {
                                if (files.length > 1) {
                                    showMsg("只允许上传一个文件！", "alert");
                                    return;
                                }
                            },
                            'BeforeUpload': function(up, file) {
                                showMsg("正在上传...", 'info');
                            },
                            'FileUploaded': function(up, file, info) {
                                var domain = up.getOption('domain');
                                info = JSON.parse(info);
                                $scope.avatarUrl = domain + info.key;
                            },
                            'Error': function(up, err, errTip) {
                                showMsg(err.message, 'alert');
                            },
                        }
                    });
                    return uploader;
                };
                $timeout(initUploader, 400);
                $scope.updateInfo = function() {
                    if ($scope.phone && $scope.mail) {
                        var user = new User();
                        user.postUpdateInfo({
                            phone: $scope.phone,
                            mail: $scope.mail,
                            avatar: $scope.avatarUrl ? $scope.avatarUrl : undefined
                        }).then(function(data) {
                            if (data.toString() === "true") {
                                showMsg('更新成功!', "info");
                                window.location.reload();
                            } else {
                                showMsg('发生错误!', "alert");
                            }
                        });
                    } else {
                        showMsg('信息填写不完整!', "alert");
                    }
                };
            }
        ]);
    });
